home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- __revision__ = '$Id: error.py 648 2006-08-26 20:09:37Z jajcus $'
- __docformat__ = 'restructuredtext en'
- import libxml2
- from pyxmpp.utils import from_utf8, to_utf8
- from pyxmpp.xmlextra import common_doc, common_root, common_ns
- from pyxmpp import xmlextra
- from pyxmpp.exceptions import ProtocolError
- stream_errors = {
- u'bad-format': ('Received XML cannot be processed',),
- u'bad-namespace-prefix': ('Bad namespace prefix',),
- u'conflict': ('Closing stream because of conflicting stream being opened',),
- u'connection-timeout': ('Connection was idle too long',),
- u'host-gone': ('Hostname is no longer hosted on the server',),
- u'host-unknown': ('Hostname requested is not known to the server',),
- u'improper-addressing': ('Improper addressing',),
- u'internal-server-error': ('Internal server error',),
- u'invalid-from': ('Invalid sender address',),
- u'invalid-id': ('Invalid stream ID',),
- u'invalid-namespace': ('Invalid namespace',),
- u'invalid-xml': ('Invalid XML',),
- u'not-authorized': ('Not authorized',),
- u'policy-violation': ('Local policy violation',),
- u'remote-connection-failed': ('Remote connection failed',),
- u'resource-constraint': ('Remote connection failed',),
- u'restricted-xml': ('Restricted XML received',),
- u'see-other-host': ('Redirection required',),
- u'system-shutdown': ('The server is being shut down',),
- u'undefined-condition': ('Unknown error',),
- u'unsupported-encoding': ('Unsupported encoding',),
- u'unsupported-stanza-type': ('Unsupported stanza type',),
- u'unsupported-version': ('Unsupported protocol version',),
- u'xml-not-well-formed': ('XML sent by client is not well formed',) }
- stanza_errors = {
- u'bad-request': ('Bad request', 'modify', 400),
- u'conflict': ('Named session or resource already exists', 'cancel', 409),
- u'feature-not-implemented': ('Feature requested is not implemented', 'cancel', 501),
- u'forbidden': ('You are forbidden to perform requested action', 'auth', 403),
- u'gone': ('Recipient or server can no longer be contacted at this address', 'modify', 302),
- u'internal-server-error': ('Internal server error', 'wait', 500),
- u'item-not-found': ('Item not found', 'cancel', 404),
- u'jid-malformed': ('JID malformed', 'modify', 400),
- u'not-acceptable': ('Requested action is not acceptable', 'modify', 406),
- u'not-allowed': ('Requested action is not allowed', 'cancel', 405),
- u'not-authorized': ('Not authorized', 'auth', 401),
- u'payment-required': ('Payment required', 'auth', 402),
- u'recipient-unavailable': ('Recipient is not available', 'wait', 404),
- u'redirect': ('Redirection', 'modify', 302),
- u'registration-required': ('Registration required', 'auth', 407),
- u'remote-server-not-found': ('Remote server not found', 'cancel', 404),
- u'remote-server-timeout': ('Remote server timeout', 'wait', 504),
- u'resource-constraint': ('Resource constraint', 'wait', 500),
- u'service-unavailable': ('Service is not available', 'cancel', 503),
- u'subscription-required': ('Subscription is required', 'auth', 407),
- u'undefined-condition': ('Unknown error', 'cancel', 500),
- u'unexpected-request': ('Unexpected request', 'wait', 400) }
- legacy_codes = {
- 302: 'redirect',
- 400: 'bad-request',
- 401: 'not-authorized',
- 402: 'payment-required',
- 403: 'forbidden',
- 404: 'item-not-found',
- 405: 'not-allowed',
- 406: 'not-acceptable',
- 407: 'registration-required',
- 408: 'remote-server-timeout',
- 409: 'conflict',
- 500: 'internal-server-error',
- 501: 'feature-not-implemented',
- 502: 'service-unavailable',
- 503: 'service-unavailable',
- 504: 'remote-server-timeout',
- 510: 'service-unavailable' }
- STANZA_ERROR_NS = 'urn:ietf:params:xml:ns:xmpp-stanzas'
- STREAM_ERROR_NS = 'urn:ietf:params:xml:ns:xmpp-streams'
- PYXMPP_ERROR_NS = 'http://pyxmpp.jajcus.net/xmlns/errors'
- STREAM_NS = 'http://etherx.jabber.org/streams'
-
- class ErrorNode:
-
- def __init__(self, xmlnode_or_cond, ns = None, copy = True, parent = None):
- if type(xmlnode_or_cond) is str:
- xmlnode_or_cond = unicode(xmlnode_or_cond, 'utf-8')
-
- self.xmlnode = None
- self.borrowed = 0
- if isinstance(xmlnode_or_cond, libxml2.xmlNode):
- self._ErrorNode__from_xml(xmlnode_or_cond, ns, copy, parent)
- elif isinstance(xmlnode_or_cond, ErrorNode):
- if not copy:
- raise TypeError, 'ErrorNodes may only be copied'
-
- self.ns = from_utf8(xmlnode_or_cond.ns.getContent())
- self.xmlnode = xmlnode_or_cond.xmlnode.docCopyNode(common_doc, 1)
- if not parent:
- parent = common_root
-
- parent.addChild(self.xmlnode)
- elif ns is None:
- raise ValueError, 'Condition namespace not given'
- elif parent:
- self.xmlnode = parent.newChild(common_ns, 'error', None)
- self.borrowed = 1
- else:
- self.xmlnode = common_root.newChild(common_ns, 'error', None)
- cond = self.xmlnode.newChild(None, to_utf8(xmlnode_or_cond), None)
- ns = cond.newNs(ns, None)
- cond.setNs(ns)
- self.ns = from_utf8(ns.getContent())
-
-
- def __from_xml(self, xmlnode, ns, copy, parent):
- if not ns:
- ns = None
- c = xmlnode.children
- while c:
- ns = c.ns().getContent()
- if ns in (STREAM_ERROR_NS, STANZA_ERROR_NS):
- break
-
- ns = None
- c = c.next
- if ns == None:
- raise ProtocolError, 'Bad error namespace'
-
-
- self.ns = from_utf8(ns)
- if copy:
- self.xmlnode = xmlnode.docCopyNode(common_doc, 1)
- if not parent:
- parent = common_root
-
- parent.addChild(self.xmlnode)
- else:
- self.xmlnode = xmlnode
- self.borrowed = 1
- if copy:
- ns1 = xmlnode.ns()
- xmlextra.replace_ns(self.xmlnode, ns1, common_ns)
-
-
-
- def __del__(self):
- if self.xmlnode:
- self.free()
-
-
-
- def free(self):
- if not self.borrowed:
- self.xmlnode.unlinkNode()
- self.xmlnode.freeNode()
-
- self.xmlnode = None
-
-
- def free_borrowed(self):
- self.xmlnode = None
-
-
- def is_legacy(self):
- return not self.xmlnode.hasProp('type')
-
-
- def xpath_eval(self, expr, namespaces = None):
- ctxt = common_doc.xpathNewContext()
- ctxt.setContextNode(self.xmlnode)
- ctxt.xpathRegisterNs('ns', to_utf8(self.ns))
- if namespaces:
- for prefix, uri in namespaces.items():
- ctxt.xpathRegisterNs(prefix, uri)
-
-
- ret = ctxt.xpathEval(expr)
- ctxt.xpathFreeContext()
- return ret
-
-
- def get_condition(self, ns = None):
- if ns is None:
- ns = self.ns
-
- c = self.xpath_eval('ns:*')
- if not c:
- self.upgrade()
- c = self.xpath_eval('ns:*')
-
- if not c:
- return None
-
- if ns == self.ns and c[0].name == 'text':
- if len(c) == 1:
- return None
-
- c = c[1:]
-
- return c[0]
-
-
- def get_text(self):
- c = self.xpath_eval('ns:*')
- if not c:
- self.upgrade()
-
- t = self.xpath_eval('ns:text')
- if not t:
- return None
-
- return from_utf8(t[0].getContent())
-
-
- def add_custom_condition(self, ns, cond, content = None):
- c = self.xmlnode.newTextChild(None, to_utf8(cond), content)
- ns = c.newNs(to_utf8(ns), None)
- c.setNs(ns)
- return c
-
-
- def upgrade(self):
- if not self.xmlnode.hasProp('code'):
- code = None
- else:
-
- try:
- code = int(self.xmlnode.prop('code'))
- except (ValueError, KeyError):
- code = None
-
- if code and legacy_codes.has_key(code):
- cond = legacy_codes[code]
- else:
- cond = None
- condition = self.xpath_eval('ns:*')
- if condition:
- return None
- elif cond is None:
- condition = self.xmlnode.newChild(None, 'undefined-condition', None)
- ns = condition.newNs(to_utf8(self.ns), None)
- condition.setNs(ns)
- condition = self.xmlnode.newChild(None, 'unknown-legacy-error', None)
- ns = condition.newNs(PYXMPP_ERROR_NS, None)
- condition.setNs(ns)
- else:
- condition = self.xmlnode.newChild(None, cond, None)
- ns = condition.newNs(to_utf8(self.ns), None)
- condition.setNs(ns)
- txt = self.xmlnode.getContent()
- if txt:
- text = self.xmlnode.newTextChild(None, 'text', txt)
- ns = text.newNs(to_utf8(self.ns), None)
- text.setNs(ns)
-
-
-
- def downgrade(self):
- if self.xmlnode.hasProp('code'):
- return None
-
- cond = self.get_condition()
- if not cond:
- return None
-
- cond = cond.name
- if stanza_errors.has_key(cond) and stanza_errors[cond][2]:
- self.xmlnode.setProp('code', to_utf8(stanza_errors[cond][2]))
-
-
-
- def serialize(self):
- return self.xmlnode.serialize(encoding = 'utf-8')
-
-
-
- class StreamErrorNode(ErrorNode):
-
- def __init__(self, xmlnode_or_cond, copy = 1, parent = None):
- if type(xmlnode_or_cond) is str:
- xmlnode_or_cond = xmlnode_or_cond.decode('utf-8')
-
- if type(xmlnode_or_cond) is unicode:
- if not stream_errors.has_key(xmlnode_or_cond):
- raise ValueError, 'Bad error condition'
-
-
- ErrorNode.__init__(self, xmlnode_or_cond, STREAM_ERROR_NS, copy = copy, parent = parent)
-
-
- def get_message(self):
- cond = self.get_condition()
- if not cond:
- self.upgrade()
- cond = self.get_condition()
- if not cond:
- return None
-
-
- cond = cond.name
- if not stream_errors.has_key(cond):
- return None
-
- return stream_errors[cond][0]
-
-
-
- class StanzaErrorNode(ErrorNode):
-
- def __init__(self, xmlnode_or_cond, error_type = None, copy = 1, parent = None):
- if type(xmlnode_or_cond) is str:
- xmlnode_or_cond = unicode(xmlnode_or_cond, 'utf-8')
-
- if type(xmlnode_or_cond) is unicode:
- if not stanza_errors.has_key(xmlnode_or_cond):
- raise ValueError, 'Bad error condition'
-
-
- ErrorNode.__init__(self, xmlnode_or_cond, STANZA_ERROR_NS, copy = copy, parent = parent)
- if type(xmlnode_or_cond) is unicode:
- if error_type is None:
- error_type = stanza_errors[xmlnode_or_cond][1]
-
- self.xmlnode.setProp('type', to_utf8(error_type))
-
-
-
- def get_type(self):
- if not self.xmlnode.hasProp('type'):
- self.upgrade()
-
- return from_utf8(self.xmlnode.prop('type'))
-
-
- def upgrade(self):
- ErrorNode.upgrade(self)
- if self.xmlnode.hasProp('type'):
- return None
-
- cond = self.get_condition().name
- if stanza_errors.has_key(cond):
- typ = stanza_errors[cond][1]
- self.xmlnode.setProp('type', typ)
-
-
-
- def get_message(self):
- cond = self.get_condition()
- if not cond:
- self.upgrade()
- cond = self.get_condition()
- if not cond:
- return None
-
-
- cond = cond.name
- if not stanza_errors.has_key(cond):
- return None
-
- return stanza_errors[cond][0]
-
-
-